package ferp.android.dialogs;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.managers.ProfileManager;
import ferp.core.game.Settings;
import ferp.core.player.Profile;
import ferp.core.tutorial.Scenario;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * User: igorgok
 * Date: 15/07/2014 11:04
 */
public class TutorialDialog extends RadioListViewDialog
{
  public TutorialDialog(Context context, final Listener listener, final int requestCode)
  {
    super(context, listener, requestCode);

    play = resolveButton(R.id.dlg_element_footer_button, R.string.dialog_tutorial_play);

    play.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View view)
      {
        // avoid double processing
        if (play.isClickable())
        {
          play.setClickable(false);

          for (int i = 0; i < ld.list.getCount(); ++i)
            if (ld.list.isItemChecked(i))
            {
              TutorialDialog.super.onItemClicked(i);

              return;
            }
        }
      }
    });

    setOnShowListener(new OnShowListener()
    {
      @Override
      public void onShow(DialogInterface dialogInterface)
      {
        // restore clickable state
        play.setClickable(true);
      }
    });
  }

  public void prepare(ProfileManager.Tutorial tm, ProfileManager.Regular rm)
  {
    Profile tutorial = tm.getCurrentProfile();
    Profile regular = rm.getCurrentProfile();
    Settings.Locale locale = regular == null ? Settings.Locale.locale(Locale.getDefault().getLanguage()) : regular.settings.locale;
    List<String> names = new LinkedList<String>();
    int id = 0;

    for (String name : Scenario.getNames(locale))
    {
      Status status = Status.get(getContext(), tm, id++);

      names.add(String.format(FORMAT, name, status.color, status.description));
    }

    set(getContext().getString(R.string.dialog_tutorial_title), names, tutorial == null ? ListView.INVALID_POSITION : Integer.valueOf(tutorial.id));

    GUI.enableButton(play, tutorial != null);
  }

  @Override
  protected int getFooterVisibility() {return View.VISIBLE;}

  @Override
  public void onItemClicked(int i) {GUI.enableButton(play, true);}

  private final static String FORMAT = "%s<br><small><font color=\"%s\">%s</font></small>";

  private Button play;

  private static final class Status
  {
    private static Status get(Context context, ProfileManager.Tutorial tm, int id)
    {
      Profile tutorial = tm.find(String.valueOf(id));
      Profile.TutorialStatus ts = tutorial == null ? Profile.TutorialStatus.NOT_STARTED : tutorial.tutorialStatus;

      return new Status(context, ts);
    }

    private Status(Context context, Profile.TutorialStatus ts)
    {
      switch (ts)
      {
        case NOT_STARTED:
          description = context.getString(R.string.tutorial_status_not_started);
          color = GUI.Color.TUTORIAL_NOT_STARTED;
          break;

        case IN_PROGRESS:
          description = context.getString(R.string.tutorial_status_in_progress);
          color = GUI.Color.TUTORIAL_IN_PROGRESS;
          break;

        default:
          description = context.getString(R.string.tutorial_status_completed);
          color = GUI.Color.TUTORIAL_COMPLETED;
          break;
      }
    }

    private String description;
    private int color;
  }
}
